use actix_web::{get, post, web::{self, ServiceConfig}};
use serde::Deserialize;

use crate::{store::pro::{self, Pro}, web::utils::{new_cbor, Cbor, CborRet, Void, VoidRet}};


#[derive(Debug, Deserialize)]
struct ProCreateReq {
    name: String,
    description: String,
}

#[post("/create")]
async fn on_create(req: Cbor<ProCreateReq>) -> CborRet<i64> {
    let id = pro::create(&req.name, &req.description).await?;
    new_cbor(id)
}

#[get("/query")]
async fn on_query() -> CborRet<Box<[Pro]>> {
    let pros = pro::query().await?;
    new_cbor(pros)
}

#[post("/del")]
async fn on_del(req: Cbor<i64>) -> VoidRet {
    pro::del(*req).await?;
    Ok(Void)
}

pub fn register(cfg: &mut ServiceConfig) {
    let scope = web::scope("/pro")
    .service(on_create)
    .service(on_query)
    .service(on_del)
    ;

    cfg.service(scope);
}
